require(["config"], function () {
    require(["jquery", "template", "bootstrap", "common"], function ($, template) {
        function Bill() {
            this.addListener();
            this.loadData(1);
        }

        $.extend(Bill.prototype, {
            addListener: function () {
                $(".btn-add-bill").on("click", $.proxy(this.addBillHandler, this));
                $(".bills-body").on("click",".del",$.proxy(this.delHandler,this));
                $(".pagination").on("click",".page",$.proxy(this.loadDataHandler,this));
                $(".pagination").on("click",".prev,.next",$.proxy(this.prevNextHandler,this));
                $(".bills-body").on("click",".update",$.proxy(this.updateform,this));
                $(".btn-up-bill").on("click",$.proxy(this.updateHandler,this));
                $(".btn-find").on("click",$.proxy(this.searchHandler,this));
            },

            addBillHandler: function () {

                var url = "/api/bill/save";

                var data = $(".form-bill").serialize();

                console.log(data);
                $.post(url, data, function (data) {
                    if (data.res_body.status === 1) {
                        var html = "";

                        var data = {
                            data: [data.res_body.data]
                        };
                        console.log(data)
                        html = template("bill_template", data);

                        $(".bills-body").append(html);
                    }
                }, "json")
            },

            loadData:function(page){
                page = page || 1;
                const url = "/api/bill/findbypage?page=" + page;

                $.getJSON(url,(data)=>{
                    if(data.res_code === 1){
                        let html = "";

                        var data = {list : data.res_body.list};

                        html = template("bills_template",data);

                        $(".bills-body").html(html);
                    }
                })
            },

            delHandler: function(e){
                $src = $(e.target);
                var _id = $src.parents("tr").find("._id").text();

                $.post("/api/bill/delete",{_id},function(data){
                    if(data.res_body.status === 1){
                        $src.parents("tr").remove();
                    }
                },"json")
            },
            loadDataHandler: function(e){
                $src = $(e.target);
                // console.log($src);

                var text = $src.text();
                this.loadData(text);
                $src.parent("li").addClass("active").siblings("li").removeClass("active");

            },
            prevNextHandler: function(e){
                var $src = $(e.target);
                // if($src)
                // console.log($src.hasClass("next"));
                if($src.hasClass("next")){
                    var page = $src.parent("li").siblings(".active").text();
                    page = Number(page) + 1;
                    // console.log(page);
                    this.loadData(page);

                    $src.parent("li").siblings(".active").removeClass("active").next().addClass("active");
                    
                    
                }else if($src.hasClass("prev")){
                    var page = $src.parent("li").siblings(".active").text();
                    page = Number(page) - 1;
                    this.loadData(page);
                    $src.parent("li").siblings(".active").removeClass("active").prev().addClass("active");

                }

            },

            updateform:function(e){
                var $src = $(e.target);
                $("#upid").val($src.parents("tr").find("._id").text());
                $("#upproname").val($src.parents("tr").find(".proname").text());
                $("#upprounit").val($src.parents("tr").find(".prounit").text());

                $("#upnumber").val($src.parents("tr").find(".number").text());
                $("#upmoney").val($src.parents("tr").find(".money").text());
                $("#updatatime").val($src.parents("tr").find(".datatime").text());
            },


            updateHandler: function(){

            },

            searchHandler: function(){
                const url = "/api/bill/findbyname";

                const  data = $(".search").serialize();

                $.get(url,data,function(data){
                    if(data.res_body.status === 1){
                        var html = "";
                        // console.log(data.res_body.data);
                        var data = {list:data.res_body.data};

                        html = template("bills_template",data);

                        $(".bills-body").html(html);
                    }
                },"json")
                return false;
            }


            
        })

        new Bill();
    })
})